import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wanandroid_flutter/utils/user.dart';
import 'package:wanandroid_flutter/utils/wan_text_style.dart';

import '../controller/mine_controller.dart';
import 'login_page.dart';

/// 我的页面
class MinePage extends StatelessWidget {
  MinePage({super.key});

  final MineController mineCtr = Get.put(MineController());

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Stack(children: [
        Image.asset("assets/images/person_background.png",
            width: double.infinity, height: 220, fit: BoxFit.fill),
        headWidget(),
        Container(
          alignment: Alignment.topRight,
          padding: const EdgeInsets.only(top: 120, right: 20),
          child: Column(children: [
            Obx(() => Text(mineCtr.userCoinCount.toString(),
                style: WanTextStyle.white_15)),
            const Text("积分", style: WanTextStyle.white_15)
          ]),
        )
      ]),
      const Spacer(),
      Container(
          margin: const EdgeInsets.symmetric(horizontal: 100, vertical: 50),
          width: double.infinity,
          child: showLoginOutBtn()
      )
    ]);
  }

  ///根据是否登录判断是否显示退出登录按钮
  Widget showLoginOutBtn() {
    return Obx(() {
      if (mineCtr.userName.value == "" || mineCtr.userName.value == "登录") {
        return const SizedBox.shrink();
      } else {
        return   RawMaterialButton(
            shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(20))),
            onPressed: () {
              //退出登录
              Get.to(LoginPage());
              User().logout();
              mineCtr.clearLoginData();
            },
            fillColor: Get.theme.colorScheme.primary,
            child: const Text("退出登录", style: TextStyle(color: Colors.white)));
      }
    });
  }

  ///头像组合控件
  Widget headWidget() {
    return Padding(
        padding: const EdgeInsets.only(top: 95, left: 40),
        child: GestureDetector(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                      width: 85,
                      height: 85,
                      decoration: BoxDecoration(boxShadow: const [
                        BoxShadow(color: Colors.grey, blurRadius: 5)
                      ], borderRadius: BorderRadius.circular(40)),
                      child: Image.asset(
                        "assets/images/friend_head.png",
                        fit: BoxFit.cover,
                      )),
                  const Padding(padding: EdgeInsets.only(top: 10)),
                  Obx(() {
                    return Text(mineCtr.userName.value);
                  }),
                ]),
            onTap: () async => {
                  if (!(await User().isLogin()))
                    {
                      //跳转到登录页面
                      Get.to(LoginPage())
                    }
                }));
  }
}
